package com.gupao.p7store.system.service.impl

import com.gupao.p7store.common.core.domain.entity.SysRole
import com.gupao.p7store.common.core.domain.entity.SysUser
import com.gupao.p7store.system.mapper.SysRoleMapper
import com.gupao.p7store.system.mapper.SysUserMapper
import com.gupao.p7store.system.mapper.SysUserPostMapper
import com.gupao.p7store.system.mapper.SysUserRoleMapper
import spock.lang.Specification

/**
 * (这里用一句话描述这个类的作用)
 * @ClassName: SysUserServiceImplSpec
 * @Author dengbicheng
 * @Date 2021/11/30 19:18
 * @Version v 1.0
 */
class SysUserServiceImplSpec extends Specification {
    /**
     * mapper mock对象
     */
    def sysUserMapper = Mock(SysUserMapper)

    /**
     * 用户职位mapper mock对象
     */
    def userPostMapper = Mock(SysUserPostMapper)

    /**
     * 用户角色mapper mock对象
     */
    def userRoleMapper = Mock(SysUserRoleMapper)

    /**
     * 角色mapper mock对象
     */
    def roleMapper = Mock(SysRoleMapper)

    /**
     * 用户服务实现类
     */
    def sysUserService = new SysUserServiceImpl(
        baseMapper: sysUserMapper,
        userPostMapper: userPostMapper,
        userRoleMapper:userRoleMapper,
        roleMapper:roleMapper
    )

    /**
     * 初始化
     */
    void setup() {

    }

    /**
     * 测试用户创建
     */
    def "测试insertUser"(){
        given:
           def sysUser = new SysUser(deptId: 11, userName: "jeffy",nickName: "jeffy chen ",postIds: [1,2],roleIds: [1,2,3])
           //mock 返回
           sysUserMapper.insert(sysUser) >> 1
           userPostMapper.insertAll(_) >> 1
           userRoleMapper.insertAll(_) >> 1
        when:
           def  result = sysUserService.insertUser(sysUser);
        then:
            result == 1
    }

    /**
     * 测试查询用户角色
     */
    def "测试selectUserRoleGroup"(){
        given: "mock 返回"
            roleMapper.selectRolesByUserName(_)>>[new SysRole(roleId:1,roleName:"ADMIN"), new SysRole(roleId:2,roleName:null)]
        when:
            def result =sysUserService.selectUserRoleGroup("admin");
        then:
            result=="ADMIN,null"

    }
}
